import datetime
import os

from PIL import Image


def paste_watermark(path_to_doc, path_to_save):
    wm_path = f"files/common/watermark.png"
    with Image.open(path_to_doc) as image:
        with Image.open(wm_path) as wm:
            w = image.size[0]
            h = image.size[1]
            wm_w = wm.size[0]
            wm_h = wm.size[1]
            coeff = w / wm_w
            wm = wm.resize((int(wm_w * coeff), int(wm_h * coeff)))
            wm = wm.crop((0, 0, w, h))
            image.paste(wm, box=(0, 0), mask=wm)
        image.save(path_to_save)


def compare_date(time_peroid: str, target_date: str):
    try:
        year = int(time_peroid.split(".")[2])
        month = int(time_peroid.split(".")[1])
        day = int(time_peroid.split(".")[0])

        compare = datetime.date(year=year, month=month, day=day)

        year = int(target_date.split(".")[2])
        month = int(target_date.split(".")[1])
        day = int(target_date.split(".")[0])

        target = datetime.date(year=year, month=month, day=day)

        if compare <= target and target <= datetime.date.today():
            return True
        else:
            return False
    except:
        return False
